{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import necessary modules\n",
    "from classy import Class\n",
    "from math import pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#####################################################\n",
    "#\n",
    "# Cosmological parameters and other CLASS parameters\n",
    "#\n",
    "#####################################################\n",
    "common_settings = {# LambdaCDM parameters\n",
    "                   'h':0.67810,\n",
    "                   'omega_b':0.02238280,\n",
    "                   'omega_cdm': 0.1201075,\n",
    "                   'A_s':2.100549e-09,\n",
    "                   'tau_reio': 0.05430842}\n",
    "\n",
    "l_max_scalars = 3000\n",
    "l_max_tensors = 600\n",
    "\n",
    "# Note that for l_max_tensors =600 we can keep default precision,\n",
    "# while for for l_max_tensors = 3000 we would need to import many high precision settings from the file cl_ref.pre    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###############\n",
    "#    \n",
    "# call CLASS : scalars only\n",
    "#\n",
    "###############\n",
    "#\n",
    "M = Class()\n",
    "M.set(common_settings)\n",
    "M.set({'output':'tCl,pCl','modes':'s','lensing':'no','n_s':0.9660499,\n",
    "       'l_max_scalars':l_max_scalars})\n",
    "M.compute()\n",
    "cls = M.raw_cl(l_max_scalars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###############\n",
    "#    \n",
    "# call CLASS : tensors only\n",
    "#\n",
    "###############\n",
    "#\n",
    "M.empty() # reset input parameters to default, before passing a new parameter set\n",
    "M.set(common_settings)\n",
    "M.set({'output':'tCl,pCl','modes':'t','lensing':'no','r':0.1,'n_t':0,\n",
    "       'l_max_tensors':l_max_tensors})\n",
    "M.compute()\n",
    "clt = M.raw_cl(l_max_tensors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###############\n",
    "#    \n",
    "# call CLASS : scalars + tensors (only in this case we can get the correct lensed ClBB)\n",
    "#\n",
    "###############\n",
    "#\n",
    "M.empty() # reset input parameters to default, before passing a new parameter set\n",
    "M.set(common_settings)\n",
    "M.set({'output':'tCl,pCl,lCl','modes':'s,t','lensing':'yes','n_s':0.9660499,'r':0.1,'n_t':0,\n",
    "       'l_max_scalars':l_max_scalars,'l_max_tensors':l_max_tensors})\n",
    "M.compute()\n",
    "cl_tot = M.raw_cl(l_max_scalars)\n",
    "cl_lensed = M.lensed_cl(l_max_scalars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# modules and esthetic definitions for the plots\n",
    "#\n",
    "# uncomment to get plots displayed in notebook\n",
    "%matplotlib inline\n",
    "#\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "#\n",
    "font = {'size'   : 16, 'family':'STIXGeneral'}\n",
    "axislabelfontsize='large'\n",
    "matplotlib.rc('font', **font)\n",
    "matplotlib.mathtext.rcParams['legend.fontsize']='medium'\n",
    "plt.rcParams[\"figure.figsize\"] = [8.0,6.0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#################\n",
    "#\n",
    "# plotting\n",
    "#\n",
    "#################\n",
    "#\n",
    "plt.xlim([2,l_max_scalars])\n",
    "plt.ylim([1.e-8,10])\n",
    "plt.xlabel(r\"$\\ell$\")\n",
    "plt.ylabel(r\"$\\ell (\\ell+1) C_l^{XY} / 2 \\pi \\,\\,\\, [\\times 10^{10}]$\")\n",
    "plt.title(r\"$r=0.1$\")\n",
    "plt.grid()\n",
    "#\n",
    "ell = cl_tot['ell']\n",
    "ellt = clt['ell']\n",
    "factor = 1.e10*ell*(ell+1.)/2./pi\n",
    "factort = 1.e10*ellt*(ellt+1.)/2./pi\n",
    "#\n",
    "plt.loglog(ell,factor*cls['tt'],'r-',label=r'$\\mathrm{TT(s)}$')\n",
    "plt.loglog(ellt,factort*clt['tt'],'r:',label=r'$\\mathrm{TT(t)}$')\n",
    "plt.loglog(ell,factor*cls['ee'],'b-',label=r'$\\mathrm{EE(s)}$')\n",
    "plt.loglog(ellt,factort*clt['ee'],'b:',label=r'$\\mathrm{EE(t)}$')\n",
    "plt.loglog(ellt,factort*clt['bb'],'g:',label=r'$\\mathrm{BB(t)}$')\n",
    "plt.loglog(ell,factor*(cl_lensed['bb']-cl_tot['bb']),'g-',label=r'$\\mathrm{BB(lensing)}$')\n",
    "plt.legend(loc='right',bbox_to_anchor=(1.4, 0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.savefig('cl_ST.pdf',bbox_inches='tight')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
